﻿using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate;
using log4net;
using NHibernate;

namespace XProject.DataVisit.DAL
{
    public class BaseSupportDal : IDisposable
    {
        protected HibernateTemplate _template;
        private ILog _log = null;
        private ISession _session;


        public HibernateTemplate Template
        {
            set { _template = value; }
            get
            {
                return _template;
            }
        }

        /// <summary>
        /// 返回一个日志记录
        /// </summary>
        public ILog Log
        {
            get
            {
                if (_log == null) _log = LogManager.GetLogger(this.GetType());
                return _log;
            }
        }

        /// <summary>
        /// 打开或者返回Session
        /// </summary>
        protected ISession Session
        {
            get
            {
                if (_session == null)
                {
                    _session = _template.SessionFactory.OpenSession();
                }
                return _session;
            }
        }

        public virtual void Close()
        {
            if (_session != null && _session.IsOpen)
                _session.Close();
        }

        #region IDisposable 成员

        public void Dispose()
        {
            Close();
        }

        #endregion
    }
}
